﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.AddIn.Hosting;
using System.Windows.Markup;
using CoreGallery.Addins.HostView;

//[assembly: XmlnsDefinition("http://coregallery.net", "CoreGallery.Core")]
namespace CoreGallery.Core.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        AddinManager addins = new AddinManager();

        public MainWindow()
        {
            InitializeComponent();
            CoreGallery.Core.Application.App.Exit += new ExitEventHandler(App_Exit);
            Init();
        }
        
        void App_Exit(object sender, ExitEventArgs e)
        {
            UnloadAllAddIns();
            GC.WaitForPendingFinalizers();
        }

        public void Init()
        {
            addins.LoadAddIns();

            foreach (var addin in addins.Importers)
            {
                InitAddIn(addin);
            }
            if (addins.Importers.Count == 0) this.AddInMenuList.Items.Add(new MenuItem() { Header = "No import addins found" });
            this.AddInMenuList.Items.Add(new Separator());

            // Publish/exporters
            foreach (var addin in addins.Exporters)
            {
                InitAddIn(addin);
            }
            if (addins.Exporters.Count == 0) this.AddInMenuList.Items.Add(new MenuItem() { Header = "No publishing addins found" });
            this.AddInMenuList.Items.Add(new Separator());

            // Transformations
            foreach (var addin in addins.Transforms)
            {
                InitAddIn(addin);
            }
            if (addins.Transforms.Count == 0) this.AddInMenuList.Items.Add(new MenuItem() { Header = "No transformation addins found" });
            this.AddInMenuList.Items.Add(new Separator());

            MenuItem addinManager = new MenuItem();
            addinManager.Header = "Add-Ins Manager";
            addinManager.Click += new RoutedEventHandler(addinManager_Click);
            this.AddInMenuList.Items.Add(addinManager);
        }

        void addinManager_Click(object sender, RoutedEventArgs e)
        {
            AddinManagerWindow win = new AddinManagerWindow();
            win.SetItemSource(addins);
            win.ShowDialog();
        }

        /// <summary>
        /// Setup each addin as necessary
        /// </summary>
        /// <param name="addin"></param>
        void InitAddIn(ICoreAddinBase addin)
        {
            MenuItem menuItem = new MenuItem();
            menuItem.Header = addin.Name;
            menuItem.Click += new RoutedEventHandler(addinMenuItem_Click);
            menuItem.Tag = addin;

            this.AddInMenuList.Items.Add(menuItem);
        }

        void addinMenuItem_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(((ICoreAddinBase)((MenuItem)sender).Tag).AboutText);
        }

        void UnloadAllAddIns()
        {
            // Remove from UI
            this.AddInMenuList.Items.Clear();

            // Remove from memory collection
            addins.UnloadAllAddIns();
        }
 
    }
}